package linc.cool.framework.protocol.dubbo;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import linc.cool.framework.Invocation;
import linc.cool.framework.register.LocalRegister;

import java.lang.reflect.Method;

/**
 * @author yqlin
 * @date 2022/3/11 4:42 下午
 * @description
 */
public class NettyServerHandler extends SimpleChannelInboundHandler<Invocation> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Invocation invocation) throws Exception {
        Class<?> serviceImpl = LocalRegister.get(invocation.getInterfaceName());
        Method method = serviceImpl.getMethod(invocation.getMethodName(), invocation.getParamTypes());
        Object result = method.invoke(serviceImpl.getDeclaredConstructor().newInstance(), invocation.getParams());
        System.out.println("Netty Read: " + result.toString());
        ctx.writeAndFlush(result);
    }
}

